package com.sl.crawler.common.utils.object;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/1/28.
 */
public class ConvertUtils {


    /**
     * Java对象转Java对象
     *
     * @param source
     * @param clazz
     * @return
     */
    public static Object transfer(Object source, Class clazz) {
        //将Orders转成OrderInfo
        String json = JSON.toJSONString(source);
        Object obj = JSON.parseObject(json, clazz);
        return obj;
    }

    /**
     * Java对象转Java对象
     *
     * @param source
     * @param typeReference
     * @return
     */
    public static Object transfer(Object source, TypeReference typeReference) {
        //将Orders转成OrderInfo
        String json = JSON.toJSONString(source);
        Object obj = JSON.parseObject(json, typeReference);
        return obj;
    }


    public static List transfer(List source, Class clazz) {
        //将Orders转成OrderInfo
        String json = JSON.toJSONString(source);
        List obj = JSON.parseArray(json, clazz);
        return obj;
    }


    private static String[] mapToArray(Map<String, String> map) {
        String[] resultArray = new String[map.size()];
        List<String> mapValuesList = new ArrayList<>(map.values());
        return mapValuesList.toArray(resultArray);
    }

    public static List<String[]> mapListToArrayList(List<Map<String, String>> mapList) {
        List<String[]> result = new ArrayList<>();
        for (Map<String, String> map : mapList) {
            String[] array = mapToArray(map);
            result.add(array);
        }
        return result;
    }

}
